package com.qdevelop.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import com.qdevelop.security.SecurityFactory;

public class SecurityFilter implements Filter{
	protected FilterConfig _filterConfig;
	@Override
	public void destroy() {
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,FilterChain filterChain) throws IOException, ServletException {
		HttpServletRequest httpRest = (HttpServletRequest) request;
		 SecurityFactory.getInstance().securityURLControl(getRequestUrl(httpRest), null);
		if(true){
//			httpRest.getSession().setAttribute(IQdevelopConstant.SECURITY_EORRO_FOCUS_URL_SESSION_KEY, getRequestUrl(httpRest));
//			((HttpServletResponse) response).sendRedirect(httpRest.getContextPath()+UtilsFactory.propertiesUtils().getProperty("Default_Security_login_url"));
		}
		filterChain.doFilter(request, response);
	}

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		this._filterConfig = filterConfig;
	}
	
	private String getRequestUrl(HttpServletRequest request){
		if(request.getQueryString() !=null){
			return (request.getRequestURL().toString() +  "?" + request.getQueryString());
		}else{
			return  request.getRequestURL().toString();
		}
		
	}

}
